<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/view/common/taglibs.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>${appname}</title>
<%@ include file="/WEB-INF/view/common/css_js_ace.jsp"%>
<%@ include file="/WEB-INF/view/common/css_js_ztree.jsp"%>
<script src="${ctx}/static/js/pub/sysmgr_auth.js" type="text/javascript"></script>
<script type="text/javascript">
	var setting = {
		view : {
			nameIsHTML : true,
			fontCss : getFont
		},
		data : {
			simpleData : {
				enable : true
			}
		},
		callback : {
			onClick : showOperation
		}
	};
	function getFont(treeId, node) {
		if (node.searchHit == true) {
			return {
				'color' : '#f79263'
			};
		} else if (node.hidden == '1') {
			return {
				'color' : '#999'
			};
		} else if (node.hidden == '0') {
			return {
				'color' : '#000'
			};
		} else {
			return {};
		}
	}
	var treeObj;
	var zNodes = ${resourceData};
	var refreshNode = '${refreshNode}';
	$(function() {
		treeObj = $.fn.zTree.init($("#zTree"), setting, zNodes);
		if( !(refreshNode == "")){
			var treenode = treeObj.getNodeByParam("id", refreshNode, null);
		    treeObj.expandNode(treenode, true, true, true);
		    if(refreshNode != "0"){
			    treeObj.selectNode(treenode);
			    showOperation(null,treeObj,treenode,null);
		    }
		}
	});

	function getFont(treeId, node) {
		return node.font ? node.font : {};
	}
	var operationAction = [ 
	                        [ "查看", "resourceDetail()" ],//0
							[ "修改", "resourceUpdate()" ],//1
							[ "删除", "deleteResource()" ],//2
							[ "上移", "upResource()" ],//3
							[ "下移", "downResource()" ],//4
							[ "上方添加同级菜单", "addResourceBefore()" ],//5
							[ "下方添加同级菜单", "addResourceAfter()" ],//6
							[ "添加子菜单", "addChildResource()" ],//7
							[ "下方添加链接", "addAction()" ],//8
							[ "下方添加链接分组", "addActionGroup()" ],//9
							[ "指定链接地址", "setAction()" ], //10
							[ "置为默认初始界面", "setInitAction()" ]//11
	                      ];
	var _selected_node_id;
	var _level;
	var _isparent;
	var _selectTreeNode;
	var _type;
	var _c_type;//如果子菜单是操作，就不让添加子菜单，只能添加操作。如果子菜单不是操作，则只能添加菜单
	var TYPE_ROOT = -1;//菜单
	var TYPE_LINK = 99;//操作
	var TYPE_LINK_GROUP = 85;//操作分组
	var TYPE_MENU = 1;//菜单
	var TYPE_RESOURCE = 80;//非菜单
	var productMode = false;
	var isRoot = false;
	function showOperation(event, treeId, treeNode, clickFlag) {
		$("#operation_div").empty();
		$("#form_content").empty();
		_level = treeNode.level;
		isRoot = _level==0;
		_type = treeNode.rstype;
		_selected_node_id = treeNode.id;
		_isparent = treeNode.isParent;
		if (_isparent) {
			_c_type = treeNode.children[0].rstype;
		}
		_selectTreeNode = treeNode;
		var rs_actions;
		if (isRoot && _isparent)
			return;//如果root下面已经有菜单，点击root将没有操作
		if (isRoot) {//root
			rs_actions = productMode ? [] : $.getFromArray(operationAction,
					[ 7 ]);
		}else{
			if (TYPE_LINK == _type) {//点击操作：修改 删除
				rs_actions = productMode ? [] : $.getFromArray(operationAction, [
						1, 2 ]);
			}else if (TYPE_LINK_GROUP == _type) {//点击操作：修改 删除
				rs_actions = productMode ? [] : $.getFromArray(operationAction, [
				                  1, 2,8 ]);
			} else {
				if (!_isparent) {//非操作 下方无子 
					rs_actions = productMode ? [] : $.getFromArray(operationAction,
							[ 1, 2, 3, 4, 5, 6, 7, 9, 10 ]);
				} else {
					if (_c_type == TYPE_LINK_GROUP) {//子菜单为链接或者分组
						rs_actions = productMode ? $.getFromArray(
								operationAction, [ 10 ]) : $
								.getFromArray(operationAction, [ 1, 2, 3, 4, 5,
										6,9, 10 ]);
					} else {//
						rs_actions = productMode ? [] : $.getFromArray(//子菜单为菜单
								operationAction, [ 1, 2, 3, 4, 5, 6, 7]);
					}
				}
			}
		}
		//显示操作按钮
		$("#operation_div").show();
		for ( var i = 0; i < rs_actions.length; i++) {
			var btncss = "btn btn-sm btn-primary";
			if (rs_actions[i][0].length >= 5) {
				btncss = "btn btn-sm btn-primary";
			}
			$("#operation_div")
					.append(
							"&nbsp;<input type=\"button\" class=\""+btncss+"\"  value=\" "+rs_actions[i][0]+" \" onclick=\""+rs_actions[i][1]+"\">");
			if (i != rs_actions.length - 1) {
				$("#operation_div").append("");
			}
		}
		if(!isRoot){
			resourceDetail(_type);
		}
	}
	function appendHtml(url){
		var appentTo = "form_content";
		jQuery.settings.ajax_debug = true;
		if(_selected_node_id!=null){
			$.htmlGet({
				url:url,
				success:function(html){
					$("#"+appentTo).empty();
					$("#"+appentTo).append(html);
				}
			});
		}
	}
	
	function addResourceBefore(){
		resourceAdd('addResourceBefore',TYPE_MENU);
	}
	function addResourceAfter(){
		resourceAdd('addResourceAfter',TYPE_MENU);
	}
	function addChildResource(){
		resourceAdd('addChildResource',TYPE_MENU);
	}
	function addAction(){
		resourceAdd('addAction',TYPE_LINK);
	}
	function addActionGroup(){
		resourceAdd('addActionGroup',TYPE_LINK_GROUP);
	}
	function resourceDetail(){
		appendHtml("${ctx}/pub/sysmgr/resmgr/res_detail.do?selectedNodeId="+_selected_node_id+"&type="+_type);
	}
	function resourceUpdate(){
		$("#operation_div").hide();
		appendHtml("${ctx}/pub/sysmgr/resmgr/res_update.do?selectedNodeId="+_selected_node_id+"&type="+_type);
		
	}
	function resourceAdd(operationType,type){
		$("#operation_div").hide();
		appendHtml("${ctx}/pub/sysmgr/resmgr/res_add.do?selectedNodeId="+_selected_node_id+"&operationType="+operationType+"&type="+type);
		
	}
	function deleteResource(){
		$.execute((_isparent?"此资源含有下级资源，":"")+"确实要删除此资源么?","${ctx}/pub/sysmgr/resmgr/delete_res.do?selectedNodeId="+_selected_node_id+"&type="+_type,null);
	}
	function setInitAction(){
		$.execute(null,"${ctx}/pub/sysmgr/resmgr/set_initaction.do?selectedNodeId="+_selected_node_id,null);
	}
	function upResource(){
		if(_selectTreeNode.isFirstNode)return;
		$.execute(null,"${ctx}/pub/sysmgr/resmgr/change_res_order.do?selectedNodeId="+_selected_node_id+"&type=up",null);
	}
	function downResource(){
		if(_selectTreeNode.isLastNode)return;
		$.execute(null,"${ctx}/pub/sysmgr/resmgr/change_res_order.do?selectedNodeId="+_selected_node_id+"&type=down",null);
	}
	function setAction(){
		appendHtml("${ctx}/pub/sysmgr/resmgr/res_action_set.do?selectedNodeId="+_selected_node_id);
	}
	$(document).ready(function() {
		$("#ztree").css({"height":$(window).height()*0.68,"width":$(window).width()*0.4});
	});
</script>
<style type="text/css">
.ztree li span.button.iconLock_ico_docu {
	margin-right: 2px;
	background: url('${ctx}/static/images/icon/lock.png') no-repeat scroll 0
		0 transparent;
	vertical-align: top;
	*vertical-align: middle;
}

.ztree li span.button.iconLock_ico_close,.ztree li span.button.iconLock_ico_open
	{
	margin-right: 2px;
	background: url('${ctx}/static/images/icon/lock.png') no-repeat scroll 0
		0 transparent;
	vertical-align: top;
	*vertical-align: middle
}
</style>
</head>
<body>
	<%-- <%@ include file="/WEB-INF/view/common/topbar.jsp"%> --%>
	<div class="main-container" id="main-container">
		<div class="main-container-inner">
			<%-- <%@ include file="/WEB-INF/view/common/sidebar.jsp"%> --%>
			<div class="main-content" style="margin-left: 0px;">
				<div class="breadcrumbs" id="breadcrumbs">
					<ul class="breadcrumb" style="margin-left: 4px;">
						<li><i class="icon-home home-icon"></i> 首页</li>
						<li>统一权限管理</li>
						<li>资源管理</li>
						<li class="active">资源列表</li>
					</ul>
				</div>

				<div class="page-content">
					<div class="row">
						<div class="col-xs-3">
							<div class="widget-box">
								<div class="widget-header widget-header-flat">
									<h5 class="smaller lighter">系统资源</h5>
								</div>
								<div class="widget-body">
									<div class="widget-main">
										<div class="row mg-top12"
											style="overflow:auto; height: 400px; border-top:1px solid #CCCCCC;">
											<div id="zTree" class="ztree"></div>
										</div>
									</div>
								</div>
							</div>
						</div>

						<div class="col-xs-9">
							<div class="widget-box" style="border-bottom:1px solid #FFFFFF;">
								<div id="operation_div">
									<div class="table-responsive">
										<div class="alert alert-info text-center">
											<i class="icon-exclamation-sign bigger-130"></i>
											请点击左侧预算系统资源树中的节点查看资源信息。
										</div>
									</div>
								</div>
								<div id="form_content" style="margin: 5px">
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</body>
</html>
